package com.shir.travels.ui.yulun;


import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.shir.travels.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class FragmentPinglun extends Fragment {

    private View view;
    private PinglunRecyclerAdapter adapter;

    private Spinner spinnertext;
    private List<String> spinner_list = new ArrayList<String>();

    private static  Random rand=new Random(47);
    private static void shuffle(String[] letters){
        Random rand=new Random(47);
        for(int i=0;i<letters.length;++i){
            int n1 = rand.nextInt(letters.length);
            int n2 = rand.nextInt(letters.length);
            String c;
            c = letters[n1];
            letters[n1] = letters[n2];
            letters[n2] = c;
        }
    }

    private void initData(){
        String[] comments = getResources().getStringArray(R.array.pinglun_comments);
//        shuffle(comments);
//        for(int i=0;i<comments.length;++i){
//            Log.d('123', comments[i]);
//        }
        adapter.clear();
        for(int i=rand.nextInt(comments.length);i<comments.length;++i){
            Map<Integer,Object> mp = new HashMap<>();
            if(i % 2 == 0){
                mp.put(PinglunRecyclerAdapter.ITEM_IMG,R.drawable.user2);
            }else{
                mp.put(PinglunRecyclerAdapter.ITEM_IMG,R.drawable.user3);
            }
            mp.put(PinglunRecyclerAdapter.VIEW_TYPE ,PinglunRecyclerAdapter.VIEW_LEFT);
            mp.put(PinglunRecyclerAdapter.ITEM_MSG,comments[i]);
            adapter.getListItemRes().add(mp);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_pinglun,container,false);

        final RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.recycler_view_pinglun);
        recyclerView.setNestedScrollingEnabled(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(view.getContext());
        ((LinearLayoutManager)layoutManager).setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new PinglunRecyclerAdapter();
        recyclerView.setAdapter(adapter);
        initData();

        final EditText editTextPublish = (EditText)view.findViewById(R.id.edit_text_frag_pinglun);
        Button btnPublish = (Button)view.findViewById(R.id.btn_frag_pinglun);
        btnPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = editTextPublish.getText()+"";
                Map<Integer,Object> mp =new HashMap<>();
                mp.put(PinglunRecyclerAdapter.VIEW_TYPE,PinglunRecyclerAdapter.VIEW_RIGHT);
                mp.put(PinglunRecyclerAdapter.ITEM_IMG,R.drawable.user3);
                mp.put(PinglunRecyclerAdapter.ITEM_MSG,msg);
                adapter.getListItemRes().add(mp);
                adapter.notifyDataSetChanged();
                recyclerView.smoothScrollToPosition(adapter.getListItemRes().size()-1);
                Log.d("MyDebug:", "new comment=["+msg+"]");
                editTextPublish.setText("");
            }
        });

        //第一步：定义下拉列表内容
        spinner_list.add("开元森泊度假乐园");
        spinner_list.add("莫干湖");
        spinner_list.add("莫干山");
        spinner_list.add("下渚湖湿地风景区");
        spinner_list.add("新市古镇");
        spinner_list.add("义远有机农场餐厅");

        this.spinnertext = (Spinner) view.findViewById(R.id.spinner1);
        ArrayAdapter<String> _Adapter=new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_spinner_item, spinner_list);
        this.spinnertext.setAdapter(_Adapter);
//
//        spinnertext.setOnFocusChangeListener(new Spinner.OnFocusChangeListener() {
//            public void onFocusChange(View v, boolean hasFocus) {
//                // TODO Auto-generated method stub
//                initData();
//                adapter.notifyDataSetChanged();
//            }
//        });
        spinnertext.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> argO, View argl, int arg2, long arg3) {
                initData();
                adapter.notifyDataSetChanged();
            }


            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });

        return view;
    }
}
